/**
 * 
 */
package com.soooft.tickey.security.core.social;

import com.soooft.tickey.security.core.properties.SecurityProperties;
import com.soooft.tickey.security.core.social.support.ImoocSpringSocialConfigurer;
import com.soooft.tickey.security.core.social.support.SocialAuthenticationFilterPostProcessor;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SpringSocialConfigurer;


/**
 * 社交登录配置主类
 * 
 * @author zhailiang
 *
 */
@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {

	@Autowired
	private SecurityProperties securityProperties;
	
	@Autowired(required = false)
	private ConnectionSignUp connectionSignUp;
	
	@Autowired(required = false)
	private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;

	/* (non-Javadoc)
	 * @see org.springframework.social.config.annotation.SocialConfigurerAdapter#getUsersConnectionRepository(org.springframework.social.connect.ConnectionFactoryLocator)
	 */
	@Override
	public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
		JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(createDataSource(),
				connectionFactoryLocator, Encryptors.noOpText());
		repository.setTablePrefix("fan_");
		if(connectionSignUp != null) {
			repository.setConnectionSignUp(connectionSignUp);
		}
		return repository;
	}
	
	/**
	 * 社交登录配置类，供浏览器或app模块引入设计登录配置用。
	 * @return
	 */
	@Bean
	public SpringSocialConfigurer imoocSocialSecurityConfig() {
		String filterProcessesUrl = securityProperties.getSocial().getFilterProcessesUrl();
		ImoocSpringSocialConfigurer configurer = new ImoocSpringSocialConfigurer(filterProcessesUrl);
		configurer.signupUrl(securityProperties.getBrowser().getSignUpUrl());
		configurer.setSocialAuthenticationFilterPostProcessor(socialAuthenticationFilterPostProcessor);
		return configurer;
	}

	/**
	 * 用来处理注册流程的工具类
	 * 
	 * @param connectionFactoryLocator
	 * @return
	 */
	@Bean
	public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator) {
		return new ProviderSignInUtils(connectionFactoryLocator,
				getUsersConnectionRepository(connectionFactoryLocator)) {
		};
	}

	//db.baicaierp.com:3308/tsoft_system_main  tsoft2014  tsoft_4006181021
	//localhost:3306/tsoft_system_main   root  fxl123
	private DataSource createDataSource(){
		DataSource dataSource = new DataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//		String jdbcurl = "jdbc:mysql://db.baicaierp.com:3308/tsoft_system_main?useUnicode=true&allowMultiQueries=true&characterEncoding=utf-8&useSSL=false";
//		dataSource.setUrl(jdbcurl);
//		dataSource.setUsername("tsoft2014");
//		dataSource.setPassword("tsoft_4006181021");
		String jdbcurl = "jdbc:mysql://localhost:3306/tsoft_system_main?useUnicode=true&allowMultiQueries=true&characterEncoding=utf-8&useSSL=false";
		dataSource.setUrl(jdbcurl);
		dataSource.setUsername("root");
		dataSource.setPassword("fxl123");
		dataSource.setInitialSize(2);
		dataSource.setTestOnBorrow(true);// 从池中取得链接时做健康检查，该做法十分保守
		dataSource.setMinEvictableIdleTimeMillis(1 * 60000 * 60);// 如果连接空闲超过1小时就断开
		dataSource.setTimeBetweenEvictionRunsMillis(600000);// 每十分钟验证一下连接
		if (jdbcurl.indexOf("sqlserver") != -1) {
			dataSource.setValidationQuery("select getdate()");
		} else if (jdbcurl.indexOf("mysql") != -1) {
			dataSource.setValidationQuery("select current_date()");
		}
		dataSource.setNumTestsPerEvictionRun(20);
		dataSource.setTestWhileIdle(true);// 运行ilde链接测试线程，剔除不可用的链接
		dataSource.setMaxWait(-1);
		return dataSource;
	}

}
